package cn.kbyue.exceptiondemo.service;

import cn.kbyue.exceptiondemo.common.QueryData;
import cn.kbyue.exceptiondemo.common.enums.ResponseEnum;
import cn.kbyue.exceptiondemo.dto.LicenceParam;
import cn.kbyue.exceptiondemo.entity.Licence;
import cn.kbyue.exceptiondemo.enums.LicenceTypeEnum;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class LicenceService {

    public Licence queryDetail(Long licenceId) {
        Licence licence = this.getById(licenceId);
        checkNotNull(licence);
        return licence;
    }

    public QueryData<Licence> getLicences(LicenceParam licenceParam) {
        Integer licenceType = licenceParam.getLicenceType();
        LicenceTypeEnum licenceTypeEnum = LicenceTypeEnum.parseOfNullable(licenceType);

        ResponseEnum.BAD_LICENCE_TYPE.assertNotNull(licenceTypeEnum);

        QueryData<Licence> data = new QueryData<>();
        List<Licence> licences = new ArrayList<>();
        licences.add(new Licence(1L, "上岗许可证", licenceTypeEnum.getTypeName()));
        data.setTotal(1);
        data.setRecords(licences);
        return data;
    }

    /**
     * 模拟数据库查询
     */
    private Licence getById(Long licenceId) {
        if (licenceId == 1L) {
            return new Licence(1L, "上岗许可证", "内部证书");
        }
        return null;
    }


    private void checkNotNull(Licence licence) {
        ResponseEnum.LICENCE_NOT_FOUND.assertNotNull(licence);
    }

}
